﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Easy.DAL;
using Easy.Model;

namespace Easy.BLL
{
    public class FormBaseBLL
    {
        public string Entity;
        private FormBaseDAL fbd;
        private EntityLinkDAL eld;
        public FormBaseBLL(string entity)
        {
            fbd = new FormBaseDAL(entity);
            this.Entity = entity;
            eld = new EntityLinkDAL();
        }
        public string getFormTitle(string id)
        {
            if (IsGuidByParse(id))
            {
                return fbd.getEntityName() + ":" + fbd.getRecordName(id);
            }
            else
            {
                return fbd.getEntityName() + ":" + "[创建]";
            }
        }
        public void saveEntityLink(string role, string entitylinks)
        {
            eld.cleanEntityLink(Entity, (role == "" ? Guid.Empty : new Guid(role)));
            List<EntityLink> EntityLinks = JsonConvert.DeserializeObject<List<EntityLink>>(entitylinks);
            foreach (EntityLink el in EntityLinks)
            {
                Guid EntityLinkId = Guid.NewGuid();
                eld.saveEntityLink_toSql(EntityLinkId, el.oname, Entity, (role == "" ? Guid.Empty : new Guid(role)), Guid.Empty, el.src, el.order);
                foreach (EntityLink sel in el.second)
                {
                    eld.saveEntityLink_toSql(Guid.NewGuid(), sel.oname, Entity, (role == "" ? Guid.Empty : new Guid(role)), EntityLinkId, sel.src, sel.order);
                }
            }
        }
        public List<EntityLink> getEntityLink(string role)
        {
            DataSet ds = eld.getEntityLink_fromSqlByRole(Entity, (role == "" ? Guid.Empty : new Guid(role)));
            return StructureEntityLinks(ds);
        }
        public List<EntityLink> getEntityLinkByUser(Guid systemuserid)
        {
            DataSet ds = eld.getEntityLink_fromSqlByUser(Entity, systemuserid);
            return StructureEntityLinks(ds);
        }
        public List<string> getFormStructure(string formname)
        {
            List<string> FormStructure = new List<string>();
            DataTable dt = fbd.getFormStructureSrc(Entity, (formname == "" ? "DefaultForm" : formname).ToString());
            if (dt.Rows.Count > 0)
            {
                FormStructure.Add(dt.Rows[0]["FormHtml"].ToString());
                FormStructure.Add(dt.Rows[0]["DisplayName"].ToString());
                FormStructure.Add(dt.Rows[0]["FormButton"].ToString());
                FormStructure.Add(dt.Rows[0]["FormJS_Src"].ToString());
                FormStructure.Add(dt.Rows[0]["FormJS_beforeLoad"].ToString());
                FormStructure.Add(dt.Rows[0]["FormJS_afterLoad"].ToString());
                FormStructure.Add(dt.Rows[0]["FormJS_beforeSave"].ToString());
                FormStructure.Add(dt.Rows[0]["FormJS_afterSave"].ToString());

                return FormStructure;
            }
            else
            {
                return null;
            }
        }

        public string SaveFormStructure(string formname, string formHTML,string buttons, string FormJS_Src, string FormJS_beforeLoad, string FormJS_afterLoad, string FormJS_beforeSave, string FormJS_afterSave)
        {
            try
            {
                fbd.saveFormStructureSrc(Entity, (formname == "" ? "DefaultForm" : formname), formHTML,buttons, FormJS_Src, FormJS_beforeLoad, FormJS_afterLoad, FormJS_beforeSave, FormJS_afterSave);
                return "success";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }
        public string CreateFormStructure(string formname, string formdisplayname, string formHTML, string FormJS_Src, string FormJS_beforeLoad, string FormJS_afterLoad, string FormJS_beforeSave, string FormJS_afterSave, string userid,int FormType=0)
        {
            try
            {
                fbd.CreateFormStructure(Entity, (formname == "" ? "DefaultForm" : formname),formdisplayname, formHTML, FormJS_Src, FormJS_beforeLoad, FormJS_afterLoad, FormJS_beforeSave, FormJS_afterSave, userid,FormType);
                return "success";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        public List<EntityLink> StructureEntityLinks(DataSet ds)
        {
            List<EntityLink> entitylinks = new List<EntityLink>();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow dr = ds.Tables[0].Rows[i];
                EntityLink el = new EntityLink();
                el.oid = dr["EntityLinkId"].ToString();
                el.oname = dr["EntityLinkName"].ToString();
                el.opid = dr["ParentId"].ToString();
                el.src = dr["Src"].ToString();
                el.order = dr["order"].ToString();
                el.second = new List<EntityLink>();

                Guid parentid = new Guid(dr["ParentId"].ToString());
                if (parentid == Guid.Empty)
                {
                    entitylinks.Add(el);
                }
                else
                {
                    EntityLink te = entitylinks.Find(
                         delegate(EntityLink _el)
                         {
                             return _el.oid == el.opid;
                         }
                     );
                    te.second.Add(el);
                }
            }
            return entitylinks;
        }

        static bool IsGuidByParse(string strSrc)
        {
            Guid g = Guid.Empty;
            return Guid.TryParse(strSrc, out g);
        }
    }
}
